package com.ibigcat.module.mdm.controller.admin.operatorlog;

import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.ibigcat.framework.common.pojo.PageParam;
import com.ibigcat.framework.common.pojo.PageResult;
import com.ibigcat.framework.common.pojo.CommonResult;
import com.ibigcat.framework.common.util.object.BeanUtils;
import static com.ibigcat.framework.common.pojo.CommonResult.success;

import com.ibigcat.framework.excel.core.util.ExcelUtils;

import com.ibigcat.framework.apilog.core.annotation.ApiAccessLog;
import static com.ibigcat.framework.apilog.core.enums.OperateTypeEnum.*;

import com.ibigcat.module.mdm.controller.admin.operatorlog.vo.*;
import com.ibigcat.module.mdm.dao.entity.OperatorLogDO;
import com.ibigcat.module.mdm.service.OperatorLogService;

@Tag(name = "管理后台 - 操作日志")
@RestController
@RequestMapping("/mdm/operator-log")
@Validated
public class OperatorLogController {

    @Resource
    private OperatorLogService operatorLogService;

    @PostMapping("/create")
    @Operation(summary = "创建操作日志")
    @PreAuthorize("@ss.hasPermission('mdm:operator-log:create')")
    public CommonResult<Long> createOperatorLog(@Valid @RequestBody OperatorLogSaveReqVO createReqVO) {
        return success(operatorLogService.createOperatorLog(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新操作日志")
    @PreAuthorize("@ss.hasPermission('mdm:operator-log:update')")
    public CommonResult<Boolean> updateOperatorLog(@Valid @RequestBody OperatorLogSaveReqVO updateReqVO) {
        operatorLogService.updateOperatorLog(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除操作日志")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('mdm:operator-log:delete')")
    public CommonResult<Boolean> deleteOperatorLog(@RequestParam("id") Long id) {
        operatorLogService.deleteOperatorLog(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得操作日志")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('mdm:operator-log:query')")
    public CommonResult<OperatorLogRespVO> getOperatorLog(@RequestParam("id") Long id) {
        OperatorLogDO operatorLog = operatorLogService.getOperatorLog(id);
        return success(BeanUtils.toBean(operatorLog, OperatorLogRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得操作日志分页")
    @PreAuthorize("@ss.hasPermission('mdm:operator-log:query')")
    public CommonResult<PageResult<OperatorLogRespVO>> getOperatorLogPage(@Valid OperatorLogPageReqVO pageReqVO) {
        PageResult<OperatorLogDO> pageResult = operatorLogService.getOperatorLogPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, OperatorLogRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出操作日志 Excel")
    @PreAuthorize("@ss.hasPermission('mdm:operator-log:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportOperatorLogExcel(@Valid OperatorLogPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<OperatorLogDO> list = operatorLogService.getOperatorLogPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "操作日志.xls", "数据", OperatorLogRespVO.class,
                        BeanUtils.toBean(list, OperatorLogRespVO.class));
    }

}